package cn.com.wangkai.wangkaisiji.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.wangkai.wangkaisiji.OrderActivity;
import cn.com.wangkai.wangkaisiji.R;
import cn.com.wangkai.wangkaisiji.bean.CartBean;
import cn.com.wangkai.wangkaisiji.bean.OrderBean;
import cn.com.wangkai.wangkaisiji.utils.Utils;

/**
 * Created by Administrator on 2016/11/15.
 */
public class LvAdapter extends BaseAdapter {

    private List<OrderBean> orderBeens;
    private Context context;
    private LayoutInflater inflater;
    private HashMap<String,List<CartBean>> c;
    private long mkeyTime;

    public LvAdapter(Map<String, List<CartBean>> c, Context context, List<OrderBean> orderBeens) {
        this.context = context;
        this.orderBeens = orderBeens;
        this.inflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return orderBeens.size();
    }

    @Override
    public Object getItem(int position) {
        return orderBeens.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView==null){
            convertView=inflater.inflate(R.layout.item_orderlist,null);
            holder=new ViewHolder();
            holder.tvConfirm= (TextView) convertView.findViewById(R.id.tv_confirm);
            holder.tvCreateTime= (TextView) convertView.findViewById(R.id.tv_createTime);
            holder.tvName= (TextView) convertView.findViewById(R.id.tv_name);
            holder.tvOrderId= (TextView) convertView.findViewById(R.id.tv_orderId);
            holder.tvPhone= (TextView) convertView.findViewById(R.id.tv_phone);
            convertView.setTag(holder);
        }
        holder= (ViewHolder) convertView.getTag();
        OrderBean bean= (OrderBean) getItem(position);
        holder.tvCreateTime.setText(orderBeens.get(position).getCreateTime());
        holder.tvName.setText(orderBeens.get(position).getName());
        holder.tvPhone.setText(orderBeens.get(position).getPhone());
        holder.tvOrderId.setText("订单号："+orderBeens.get(position).getOrderId());

        holder.tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.ShowDialog((Activity) context.getApplicationContext(),orderBeens.get(position).getOrderId(),orderBeens.get(position).getPrice(), new Utils.Callback() {
                    @Override
                    public void onConfirm(Dialog dialog, String message) {
                        if((System.currentTimeMillis() - mkeyTime) > 2000){
                            mkeyTime = System.currentTimeMillis();
                            Toast.makeText(context.getApplicationContext(), "再按一次确定收款", Toast.LENGTH_LONG).show();

                        }else{
                            onConfirm(dialog,orderBeens.get(position).getOrderId());
                        }
                    }

                    @Override
                    public void onCancel(Dialog dialog) {
                        dialog.dismiss();
                    }
                });
            }
        });

        return convertView;
    }

    class ViewHolder{
        TextView tvName ,tvOrderId ,tvPhone,tvCreateTime,tvConfirm;
    }
}
